package com.entrance;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;

public class FragmentUpdate {
    private Activity mContext;
    public FragmentUpdate(Context context) {
        mContext = (Activity) context;
    }

    /**
     * 根据选项卡选中状态显示对应fragment
     * @param cId 包含显示fragment的视图容器
     * @param fragment 要显示的容器的对象
     */
    public void displayFragment(int cId, Fragment fragment) {
        if (null == fragment) {
            return;
        }
        FragmentManager fragmentManager = mContext.getFragmentManager();
        FragmentTransaction fragmentTransaction = mContext.getFragmentManager().beginTransaction();
        fragmentTransaction.replace(cId, fragment)
                .setTransition(FragmentTransaction.TRANSIT_NONE)
                .commit();
        fragmentManager.executePendingTransactions();
    }

    /**
     * 随时刷新Fragment
     */
    public void updateFragment(int rID, Fragment fragment) {
        displayFragment(rID, new Fragment());
        displayFragment(rID, fragment);
    }

    public interface UpdateFragment {
        void update(Object type);
    }
}
